package cn.gson.school.controller.caiwu;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.caiwu.TuiKuanDao;
import cn.gson.school.dao.jiaoxue.ClassDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.caiwu.TuiKuanEntity;
import cn.gson.school.entity.jiaoxue.ClassEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.xitong.UserInfoEntity;
import cn.gson.school.mappers.caiwu.TuiKuanMapper;

/**
 * 退款
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/cw-tuikuan-list")
public class TuiKuanController {

	@Autowired
	private TuiKuanMapper tuikuanmapper;

	@Autowired
	private TuiKuanDao tuikuandao;// 退款
	@Autowired
	private StudentDao studentDao;// 学生详情
	@Autowired
	private ClassDao classDao;// 班级
	@Autowired
	private UserInfoDao userinfoDao;// 用户

	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> index(TuiKuanEntity tuikuan,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<TuiKuanEntity> pageData = tuikuanmapper.tuikuanlist(tuikuan);

		PageInfo<TuiKuanEntity> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	/**
	 * 表单
	 */
	@GetMapping({ "cw-add-tuikuan", "cw-add-tuikuan/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			TuiKuanEntity tuiKuan = tuikuandao.findOne(id);

			if (tuiKuan != null) {
				// 如果是编辑，则把原有用户信息传回表单jsp页面
				model.addAttribute("tuikuanEntity", tuiKuan);
				// 获取班级id
				ClassEntity classEntity = tuiKuan.getClassId();
				model.addAttribute("classEntity", classEntity);
				// 获取学生id
				StudentEntity studentEntity = tuiKuan.getStudentinfoId();
				model.addAttribute("studentEntity", studentEntity);
				//用户
				UserInfoEntity userInfoEntity = tuiKuan.getUserinfoId();
				model.addAttribute("UserInfoEntity", userInfoEntity);
			}
		}
		// 班级
		Iterable<ClassEntity> list = classDao.findByDelete(1);
		model.addAttribute("classList", list);
		// 学生
		Iterable<StudentEntity> student = studentDao.findAll();
		model.addAttribute("studentList", student);
		
		//用户
		Iterable<UserInfoEntity> userinfo = userinfoDao.findAll();
		model.addAttribute("userinfoList", userinfo);
		
		return "caiwu/cw-add-tuikuan";
	}

	/**
	 * 添加
	 */
	@PostMapping("/save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(HttpSession session,@Valid TuiKuanEntity tuiKuan, HttpServletRequest request, Model model,
			BindingResult br) throws ParseException {
		Map<String, Object> obj = new HashMap<String, Object>();
		//获取用户
		UserInfoEntity userInfoEntity =	(UserInfoEntity) session.getAttribute("userinfo");
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldError());
		} else {
			// 获取表单值
			String tuikuanid = request.getParameter("tuikuanid");// 退款id
			String studentName = request.getParameter("studentName");// 学生名称
			String className = request.getParameter("className");// 学生班级
			String tuikuanMoney = request.getParameter("tuikuanMoney");// 退款金额
			String tuikuanReason = request.getParameter("tuikuanReason");// 退款原因
			String tuikuanTime = request.getParameter("tuikuantime");// 退款时间
			String userName = request.getParameter("userName");// 操作人
			

			TuiKuanEntity tuiKuanEntity;
			if (tuikuanid == null || "".equals(tuikuanid)) {
				tuiKuanEntity = new TuiKuanEntity();
				tuiKuanEntity.setTuikuanTime(new Date());
			} else {
				tuiKuanEntity = tuikuandao.findOne(Long.valueOf(tuikuanid));
				tuiKuanEntity.setTuikuanId(Long.valueOf(tuikuanid));
			}
			/**
			 * 正则表达式
			 */
			Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
			// 添入数据库
			// 外键
			ClassEntity classEntity = classDao.findOne(Long.valueOf(className));// 学生班级
			StudentEntity studentEntity = studentDao.findOne(Long.valueOf(studentName));// 学生姓名
			//pinEntity.setUserinfoId(userInfoEntity);// 教员

			// 添加主键的
			Matcher matchdm = pattern.matcher(tuikuanMoney);
			if (matchdm.matches() == false) {
				obj.put("message", "退款金额输入有误！");
				return obj;
			} else {
				tuiKuanEntity.setTuikuanMoney(Double.valueOf(tuikuanMoney));// 退款金额
			}
			tuiKuanEntity.setTuikuanReason(tuikuanReason);// 退款原因
			tuiKuanEntity.setUserinfoId(userInfoEntity);//获取用户
			// 判断是否为时间格式--
			String path = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";// 定义匹配规则
			Pattern p = Pattern.compile(path);// 实例化Pattern
			Matcher m = p.matcher(tuikuanTime);// 验证字符串内容是否合法
			if (m.matches()) { // 使用正则验证
				System.out.println("输入的日期格式合法！");
				tuiKuanEntity.setTuikuanTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(tuikuanTime));
			} else {
				obj.put("message", "收款时间输入有误啊！");
				return obj;
			}
			// 判断是否为时间格式-----
			// 添加外键
			tuiKuanEntity.setStudentinfoId(studentEntity);// 学生姓名
			tuiKuanEntity.setClassId(classEntity);// 班级名称1
			//tuiKuanEntity.setUserinfoId(userInfoEntity);
			tuiKuanEntity.setUserinfoId(userInfoEntity);// 操作人1
			
			tuikuandao.save(tuiKuanEntity);
			System.out.println("操作成功!");
			obj.put("success", true);
		}
		return obj;
	}
}
